import 'package:flutter/material.dart';

/// @date 2020/07/24  15:09
/// @author 程志强
/// @des 导航返回拦截:WillPopScope

class FeaturesWillPopScope extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("导航返回拦截:WillPopScope"),
      ),
      body: _body(),
    );
  }

  Widget _body() {
    // 上次点击的时间
    DateTime _lastPressAt;
    return WillPopScope(
      onWillPop: () async {
        if (_lastPressAt == null ||
            DateTime.now().difference(_lastPressAt) > Duration(seconds: 1)) {
          // 两次点击间隔超过1秒，则重新计时
          _lastPressAt = DateTime.now();
          return false;
        } else {
          return true;
        }
      },
      child: Container(
        alignment: Alignment.center,
        child: Text("1秒内连续按两次返回键退出！"),
      ),
    );
  }
}
